package com.ama.service.impl;


import com.ama.dao.PersonDao;
import com.ama.pojo.Person;
import com.ama.service.PersonService;


import com.ama.utils.RedisUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PersonServiceImp implements PersonService {
	@Autowired
	PersonDao personDao;
	@Autowired
	RedisUtils redisUtils;
	@Override
	public boolean register(Person person) {
		//Person p = personDao.findPersonByName(person.getUsername());
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("username",person.getUsername());
		Person p = personDao.selectOne(wrapper);
		if(p==null){
			personDao.savePerson(person);
			//personDao.insert(p);
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Person login(String username, String password) {
		//Person p = personDao.findPersonByNameAndPassword(username, password);
		Person p = personDao.findPersonByNameAndPassword(username, password);
		if(p!=null){
			return p;
		}else{
			return null;
		}
	}

	@Override
	public void update(Person person) {
		//personDao.updatePerson(person);
		personDao.updateById(person);
		/**
		 * 更新redis中的数据
		 */
		redisUtils.set("Person"+person.getId(),person);
	}

	@Override
	public void publish(Person person) {
		//personDao.udpatePubtime(person);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		df.format(date);
		person.setPubtime(date);
		personDao.updateById(person);
		/**
		 * 更新redis中的数据
		 */
		redisUtils.set("Person"+person.getId(),person);

	}

	@Override
	public void deleteResume(Person person) {
		//person.ZeroPubtime(Person person)
		person.setPubtime(null);
		personDao.updateById(person);
		/**
		 * 更新redis中的数据
		 */
		redisUtils.set("Person"+person.getId(),person);
	}

	@Override
	public List<Person> listAllPersons() {
		//List<Person> persons = personDao.findAllPersons();
		/**
		 * mybatis-plus mysql操作
		 */
		/*QueryWrapper wrapper = new QueryWrapper();
		wrapper.isNotNull("pubtime");
		List<Person> persons = personDao.selectList(wrapper);
		return persons;*/

		/**
		 * redis+mysql
		 */
		if (redisUtils.keys("Person*").size() == 0) {
			QueryWrapper wrapper = new QueryWrapper();
			wrapper.isNotNull("pubtime");
			List<Person> persons = personDao.selectList(wrapper);
			int i = 0;
			while (i < persons.size()) {
				redisUtils.set("Person" + persons.get(i).getId(), persons.get(i));
				i++;
			}
			return persons;
		} else {
			Set<String> redisPerson = redisUtils.keys("Person*");
			List<Person> personList = new ArrayList<>();
			for (String p : redisPerson) {
				Person person = (Person) redisUtils.get(p);
				if(person.getPubtime()!=null){
					personList.add(person);
				}
			}
			return personList;
		}
	}

	@Override
	public List<Person> listAllPersonsByParams(Map map) {
		//List<Person> persons = personDao.findPersonsByParams(map);
		map.remove("type");
		List<Person> persons = personDao.selectByMap(map);
		return persons;
	}

	@Override
	public Person query(long id) {
		//Person person = personDao.findPersonById(id);
		Person person = personDao.selectById(id);
		return person;
	}

}
